package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import jdk.net.SocketFlow;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;


    public void saveWithDish(SetmealDTO setmealDTO){


//        套餐名称唯一
//        套餐必须属于某个分类
//        套餐必须包含菜品
//        名称、分类、价格、图片为必填项
//        添加菜品窗口需要根据分类类型来展示菜品
//        新增的套餐默认为停售状态

        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);

        setmealMapper.insert(setmeal);


        //需要维护setmeal_dish之间的关系表，所以需要获取setmeal的id，然后把所有的dish插入到这个id的表单之中
        Long setmealId  = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();

        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealId);
        });

        //保存套餐和菜品的关联关系
        setmealDishMapper.insertBatch(setmealDishes);

    }


    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO){

        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);

        long total = page.getTotal();
        List<SetmealVO> result = page.getResult();

        return new PageResult(total,result);




    }


    @Transactional
    public void deleteByIds(List<Long> ids){


        //检查是否在套餐之中

        ids.forEach(id -> {
            Setmeal setmeal = setmealMapper.getById(id);
            if(StatusConstant.ENABLE == setmeal.getStatus()){
                //起售中的套餐不能删除
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        });
//        for(Long id:ids){
//            Setmeal byId = setmealMapper.getById(id);
//            if(StatusConstant.ENABLE == byId.getStatus()){
//                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
//            }
//        }
//
//        需要从setmeal和setmealDish表中都删除

        ids.forEach(setmealId->{
            setmealMapper.deleteBatch(setmealId);
            setmealDishMapper.deleteBatch(setmealId);
        });

    }



    public SetmealVO getByIdWithDish(Long id){


        //查询setmeal的所有信息，还需要在setmeal里面搜索dish内容然后再给回显出来
        //先获取setmeal的id然后再查询所有的dishes

        SetmealVO setmealVO = new SetmealVO();
        Setmeal setmeal = setmealMapper.getById(id);


        List<SetmealDish> dishById = setmealDishMapper.getBySetmealId(id);
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(dishById);



        return setmealVO;
    }



    @Transactional
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);

        //1、修改套餐表，执行update
        setmealMapper.update(setmeal);

        //套餐id
        Long setmealId = setmealDTO.getId();

        //2、删除套餐和菜品的关联关系，操作setmeal_dish表，执行delete
        setmealDishMapper.deleteBySetmealId(setmealId);

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealId);
        });
        //3、重新插入套餐和菜品的关联关系，操作setmeal_dish表，执行insert
        setmealDishMapper.insertBatch(setmealDishes);
    }


    public void updateStatus(Integer status,Long id){

        //需要检查套餐里面的菜品是否都可以销售
        //查询setmeal——dish表格中的dish——id

        List<Dish> dishes= setmealDishMapper.getDishesBySetmealId(id);

        dishes.forEach(dish -> {
            if(dish.getStatus() == StatusConstant.DISABLE)
                throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
        });

        Setmeal byId = setmealMapper.getById(id);
        byId.setStatus(status);

        setmealMapper.update(byId);

    }


    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
